package com.qwt.part_time_api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qwt.part_time_api.mapper.CategoryMapper;
import com.qwt.part_time_api.mapper.PersonalInformationMapper;
import com.qwt.part_time_api.mapper.TaskMapper;
import com.qwt.part_time_api.pojo.Category;
import com.qwt.part_time_api.pojo.PersonalInformation;
import com.qwt.part_time_api.service.MerchantsService;
import com.qwt.part_time_api.vo.ReleaseMerchantsVo;
import com.qwt.part_time_api.vo.Result;
import com.qwt.part_time_api.vo.TaskListVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 君子慎独
 * @create 2022/1/2 0002 23:40
 */

@Service
@Slf4j
public class MerchantsServiceImpl implements MerchantsService {

    @Autowired
    private PersonalInformationMapper personalInformationMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private TaskMapper taskMapper;

    @Override
    public Result findMerchantsById(Long rid) {
        log.info("获取商家信息");
        LambdaQueryWrapper<PersonalInformation> wrapper = new LambdaQueryWrapper<>();

        wrapper.select(PersonalInformation::getId,
                PersonalInformation::getName,
                PersonalInformation::getAvatars);
        wrapper.eq(PersonalInformation::getId, rid);

        PersonalInformation personalInformation = personalInformationMapper.selectOne(wrapper);

        return Result.success(copy(personalInformation));
    }

    @Override
    public Result findTaskListByMerchantsId(Long rid) {
        log.info("获取商家推荐列表");
        List<TaskListVo> taskListByMerchantsId = taskMapper.findTaskListByMerchantsId(rid);

        return Result.success(taskListByMerchantsId);
    }


    private ReleaseMerchantsVo copy(PersonalInformation personalInformation) {
        ReleaseMerchantsVo releaseMerchantsVo = new ReleaseMerchantsVo();
        //属性相同进行 复制
        BeanUtils.copyProperties(personalInformation, releaseMerchantsVo);
        if (personalInformation.getId() != null) {
            List<Category> categoryByInfoId = categoryMapper.findCategoryByInfoId(personalInformation.getId());
            releaseMerchantsVo.setCategory(categoryByInfoId);
        }
        return releaseMerchantsVo;
    }
}
